<?php

/**
 * 处理uploadify上传
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: UploadController.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Admin\Controller;
class UploadController extends CommonController{
    public function _initialize() {
        /*$session_id = I("post.sid", "");
        
        if(empty($session_id)){
            return $this->errorReturn("非法操作!");
        }else{
            $_COOKIE['PHPSESSID'] = $session_id;
            //session_destroy();
            //setSessionId($session_id);
            $loginMarked = C('LOGIN_MARKED');
            $account = I("session.current_account");
            if(!isArrayAndNotEmpty($account)){
                return $this->errorReturn("非法操作!");
            }
            $shell = D("Admin", "Service")->genByIdShell($account["id"]);
            // 生成登录session
            setSession($loginMarked, $shell);
            // 生成登录cookie
            $shell .= '_' . time();
            setcookie($loginMarked, $shell, 0, '/');
        }
        if(empty($_SESSION)){
            return $this->errorReturn("非法操作!");
        }
        //var_dump($_SESSION, $_COOKIE);die;
        parent::_initialize();*/
    }
    
    /**
     * 处理uploadify上传图片
     */
   
    public function image(){
        //var_dump($_FILES, C('UPLOAD_IMAGE_MIMES'));
        $verifyToken = md5(C("COOKIE_ENCODE") . I('post.salt'));
        if (I('post.token') == $verifyToken) {
            $config = array(    
                'maxSize'    =>    C('UPLOAD_MAX_SIZE'),    
                'savePath'   =>    'image/',    
                'replace'    =>    true,    
                'exts'       =>    C('UPLOAD_IMAGE_EXTS')
                //'mimes'      =>    C('UPLOAD_IMAGE_MIMES') //flash上传无法正确获取mimes
            );
            $upload = $this->_upload($config);
            $mark_conf = fast_cache("inc_photowatermark_config", '', YDCMS_DATA . "mark/");
            $image = new \Think\Image(); 
            foreach ($upload as $key => $value) {
                if($mark_conf['photo_markup'] == 1){//添加水印
                   
                    $filePath = YDCMS_ROOT . C('UPLOAD_PATH') . $value['savepath'] . $value['savename'];
                    $pos = $mark_conf['photo_waterpos'] == 0 ? rand(1, 9) : $mark_conf['photo_waterpos'];
                    if(($mark_conf['photo_marktype'] == 2 || !file_exist(YDCMS_DATA . "mark/" . $mark_conf['photo_markimg']) && file_exist(YDCMS_DATA . 'mark/simhei.ttf'))){//添加文字水印
                        
                        $image->open($filePath)
                                ->text($mark_conf['photo_watertext'], YDCMS_DATA . 'mark/simhei.ttf', $mark_conf['photo_fontsize'], $mark_conf['photo_fontcolor'], $pos)
                                ->save($filePath); 
                    
                    }else if(file_exist(YDCMS_DATA . "mark/" . $mark_conf['photo_markimg'])){//添加图片水印
                        
                        $image->open($filePath)->water(YDCMS_DATA . "mark/" . $mark_conf['photo_markimg'], $pos, $mark_conf['photo_diaphaneity'])->save($filePath); 
                        
                    }
                    
                }
                
                $upload[$key]["fileurl"] = C('UPLOAD_PATH') . $value['savepath'] . $value['savename'];
            }
            $this->successReturn(array_shift($upload));
        }else{
            return $this->errorReturn("token验证错误");
        }
    }

    private function _upload($config) {
        $upload = new \Think\Upload($config);
        $info = $upload->upload();
        if(!$info){
            return $upload->getError();
        }else{
            return $info;
        }
    }
}
